# -*- coding: utf-8 -*-

from django.shortcuts import render_to_response, redirect
from django.core.urlresolvers import reverse
from django.template.context import RequestContext
from comunidadcyt.Registration_Users.models import User
from comunidadcyt.views import index as home_index

def listar_solicitudes(request):
    if request.user.is_superuser:
        request.breadcrumbs('Administración',request.path)
        usuarios = User.objects.filter(is_active=False)
        return render_to_response(
            'administracion/solicitudes.html',
            {'user': request.user, 'usuarios': usuarios},
            context_instance = RequestContext(request)
        )
    redirect(reverse(home_index(request)))

def ver_solicitud(request, iduser):
    if request.user.is_superuser:
        try:
            u = User.objects.get(id = iduser)
            request.breadcrumbs([('Administracion',reverse(listar_solicitudes)),('Solicitud de ' + u.username, request.path)])
            return render_to_response(
                'administracion/ver_solicitud.html',
                {'user': request.user, 'useredit': u},
                context_instance = RequestContext(request)
            )
        except Exception:
            pass

    redirect(reverse(listar_solicitudes(request)))

def activar(request, iduser):
    if request.user.is_superuser:
        if request.method == 'POST':
            try:
                u = User.objects.get(id = iduser)
                u.is_active = True
                u.save()
                return render_to_response(
                    'administracion/activation_success.html',
                    {'msg': "Se ha activado correctamente al usuario."},
                    context_instance = RequestContext(request)
                )
            except Exception:
                pass
    redirect(reverse(listar_solicitudes(request)))